package com.pai4j.connect.service;
import com.pai4j.common.enums.RedisKeyEnum;
import com.pai4j.common.util.DateUtils;
import com.pai4j.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class GroupVideoBizService {


    @Autowired
    private RedisUtil redisUtil;

    /**
     * 群聊多人音视频房间，上线新用户
     *
     * Sorted Set
     * @param groupId
     * @param userId
     */
    public void online(String groupId, String userId) {
        String key = getOnlineUsersRedisKey(groupId);
        redisUtil.zadd(key, userId, DateUtils.getCurrentTimeMillis(),
                RedisKeyEnum.GROUP_VIDEO_ONLINE_USER_ZSET.getExpireTime());
    }

    /**
     * 群聊多人音视频房间，上线新用户
     *
     * @param groupId
     * @param userId
     */
    public void offline(String groupId, String userId) {
        String key = getOnlineUsersRedisKey(groupId);
        redisUtil.zremove(key, userId);
    }

    /**
     * 获取群聊音视频通话所有在线用户在线
     *
     * @param groupId
     * @return
     */
    public Set<String> getOnlineUserIds(String groupId) {
        String key = getOnlineUsersRedisKey(groupId);
        return redisUtil.zrangeByScore(key, 0, Long.MAX_VALUE);
    }

    private String getOnlineUsersRedisKey(String groupId) {
        return RedisKeyEnum.GROUP_VIDEO_ONLINE_USER_ZSET.getKey(groupId);
    }
}
